package com.Math;

public class Polygon2D {
	public Vector2D[] verts;
	public int color;
	public Vector2D center;
	public double dir = Math.PI/2;

	public Polygon2D(Vector2D[] v, int cx, int cy) {
		verts = v;
		center = new Vector2D(cx, cy);
		for (int i = 0; i < v.length; i++) {
			v[i].subtract(center);
			verts[i] = v[i];
		}
	}

	public Polygon2D(Vector2D[] v) {
		verts = v;
		findCenter();
		for (int i = 0; i < v.length; i++) {
			v[i].subtract(center);
			verts[i] = v[i];
		}
	}

	public void findCenter() {
		center = new Vector2D();
		for (Vector2D v : verts) {
			center.add(v);
		}
		center.divide(verts.length);
	}

	public void move(Vector2D v) {
		center.add(v);
	}

	public void move(double x, double y) {
		center.add(x, y);
	}
	public void moveTo(Vector2D v) {
		center.x=v.x;
		center.y=v.y;
	}

	public void moveTo(double x, double y) {
		center.x=x;
		center.y=y;
	}

	public void rotateRad(double rad) {
		dir += rad;
		for (int i = 0; i < verts.length; i++) {
			double dx = verts[i].x * Math.cos(rad) - verts[i].y * Math.sin(rad);
			verts[i].y = verts[i].x * Math.sin(rad) + verts[i].y * Math.cos(rad);
			verts[i].x = dx;
		}
	}

	public static Polygon2D box(int x, int y, int width, int height) {
		Vector2D[] v = new Vector2D[4];
		v[0] = new Vector2D(x, y);
		v[1] = new Vector2D(x, y + height);
		v[2] = new Vector2D(x + width, y + height);
		v[3] = new Vector2D(x + width, y);
		return new Polygon2D(v);
	}
}
